package com.jindy.module.jpush.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.jpush.domain.Jpush;
import com.jindy.module.jpush.service.IJpushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * jpush消息推送Controller
 * 
 * @author jindy
 * @date 2023-04-02
 */
@RestController
@RequestMapping("/module/jpush")
public class JpushController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(JpushController.class);
    @Autowired
    private IJpushService jpushService;

    /**
     * 查询jpush消息推送列表
     */
    @PreAuthorize("@ss.hasPermi('module:jpush:list')")
    @GetMapping("/list")
    public TableDataInfo list(Jpush jpush)
    {
        startPage();
        List<Jpush> list = jpushService.selectJpushList(jpush);
        return getDataTable(list);
    }

    /**
     * 导出jpush消息推送列表
     */
    @PreAuthorize("@ss.hasPermi('module:jpush:export')")
    @Log(title = "jpush消息推送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jpush jpush)
    {
        List<Jpush> list = jpushService.selectJpushList(jpush);
        ExcelUtil<Jpush> util = new ExcelUtil<Jpush>(Jpush.class);
        util.exportExcel(response, list, "jpush消息推送数据");
    }

    /**
     * 获取jpush消息推送详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:jpush:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jpushService.selectJpushById(id));
    }

    /**
     * 新增jpush消息推送
     */
    // @PreAuthorize("@ss.hasPermi('module:jpush:add')")
    @Log(title = "jpush消息推送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Jpush jpush)
    {
        try {
            return toAjax(jpushService.insertJpush(jpush));
        } catch (Exception e) {
            LOGGER.error("新增jpush消息推送失败",e);
            return AjaxResult.error("新增jpush消息推送失败");
        }
    }

    /**
     * 修改jpush消息推送
     */
    @PreAuthorize("@ss.hasPermi('module:jpush:edit')")
    @Log(title = "jpush消息推送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Jpush jpush)
    {
        return toAjax(jpushService.updateJpush(jpush));
    }

    /**
     * 删除jpush消息推送
     */
    @PreAuthorize("@ss.hasPermi('module:jpush:remove')")
    @Log(title = "jpush消息推送", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jpushService.deleteJpushByIds(ids));
    }
}
